package com.project_test.kun.travelling_1st.Butler;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DialogTitle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.volley.Cache;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.project_test.kun.travelling_1st.R;
import com.project_test.kun.travelling_1st.Utils.CustomVolley;
import com.project_test.kun.travelling_1st.Utils.LListView;
import com.squareup.picasso.Picasso;

import org.json.JSONObject;

/**
 * http://www.handybest.com/index.php?m=api&c=Keeper&a=detail&kid=373&device_token=864394010925172&push_token=48c3e0cad9df6d2e4446b318711cdb7e09784448&mobile_type=Android&mobile_model=GT-N7100%2C4.4.2&version_code=10000
 */
public class ButlerDetailActivity extends AppCompatActivity implements View.OnClickListener {
    private String path = "http://www.handybest.com/index.php?m=api&c=Keeper&a=detail&kid=";
    private int kid = 373;
    private String path2 = "&device_token=864394010925172&push_token=48c3e0cad9df6d2e4446b318711cdb7e09784448&mobile_type=Android&mobile_model=GT-N7100%2C4.4.2&version_code=10000";
    private ImageView goBack, share, head, uPic;
    private TextView title, sTitle, uname, city, service_time, price_in, price_out, rating,search,back_search;
    private JSONObject json;
    private ButlerDetailAdapter adapter;
    private RequestQueue mRequestQueue;
    private Gson gson;
    private Detail detail;
    private LListView listView;
    private View view;
    private PopupWindow window;

   Handler handler=new Handler(){
       @Override
       public void handleMessage(Message msg) {
           super.handleMessage(msg);
           if (msg.what==1) {
               json = (JSONObject) msg.obj;
                   JSONObject obj1 = json.optJSONObject("data");
               gson=new Gson();
               detail = gson.fromJson(json.toString(), Detail.class);
               initData();
           }
       }
   };
    private TextView showInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_butler_detail);
        initView();
        jsonRequest(path+kid+path2);
        view = LayoutInflater.from(this).inflate(R.layout.popup, null);
        window  =  new PopupWindow(view,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        //没生效
        window.setOutsideTouchable(true); //点击popupWindow外部时候是否会关闭
        window.setFocusable(true);
        showInfo = (TextView) findViewById(R.id.text);
    }

    private void initData() {
        title.setText(detail.getData().getTitle());
        Picasso.with(this).load(detail.getData().getHead_pic()).into(uPic);
        sTitle.setText(detail.getData().getService_type_name());
        uname.setText(detail.getData().getUname());
        Picasso.with(this).load(detail.getData().getPicList().get(0)).into(head);
        city.setText(detail.getData().getCity_name());
        service_time.setText(detail.getData().getService_time());
        price_in.setText(detail.getData().getPrice_in());
        price_out.setText(detail.getData().getPrice_out());
        rating.setText("暂无评价");
        search.setClickable(true);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogTitle dialogTitle=new DialogTitle(getApplicationContext());

            }
        });

        adapter=new ButlerDetailAdapter(getApplicationContext(),detail.getData().getIntroList());
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    private void jsonRequest(final String path) {
        mRequestQueue = CustomVolley.getInstance(this).sRequestQueue;
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
                path, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject arg0) {
                Message msg = new Message();
                msg.what = 1;
                msg.obj = arg0;
                handler.sendMessage(msg);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {
                //            获取缓存
                Cache cache = mRequestQueue.getCache();
                if (cache != null) {
                    Cache.Entry entry = cache.get(path);
                    if (entry != null) {
                        byte[] data = entry.data;
                        if (data != null) {
                            try {
                                String result = new String(data, "UTF-8");
                                JSONObject obj = new JSONObject(result);
                                Message msg = new Message();
                                msg.obj = obj;
                                handler.sendMessage(msg);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        });
        // 设置标签
        jsonObjectRequest.setTag("bcdGet");

        mRequestQueue.add(jsonObjectRequest);
    }

    private void initView() {

        uPic = (ImageView) findViewById(R.id.uPic);
        goBack = (ImageView) findViewById(R.id.goBack);
        title = (TextView) findViewById(R.id.title);
        sTitle = (TextView) findViewById(R.id.sTitle);
        uname = (TextView) findViewById(R.id.butlerName);
        head = (ImageView) findViewById(R.id.head);
        listView= (LListView) findViewById(R.id.butler_list);
        city= (TextView) findViewById(R.id.city);
        service_time = (TextView) findViewById(R.id.time);
        price_in= (TextView) findViewById(R.id.include);
        price_out= (TextView) findViewById(R.id.unInclude);
        rating= (TextView) findViewById(R.id.rating);
        search= (TextView) findViewById(R.id.search);
        back_search= (TextView) findViewById(R.id.back_search);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.goBack:
                finish();
                break;
        }
    }
}
